<?php

class FIN_categoryBean 
{
	
	public static function get_cat_by_condition($db,$cat,$lang,$type,$widget) {
		$locale = $_SESSION["LC_ALL"];
		if($position!="")
			$strpo = "c.cat_type ='$type' AND "	;
    	$strSQL = sprintf("SELECT c.cat_link,c.cat_parent,cl.cat_id,c.active,cl.cat_name FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_TYPE." ct ON ct.cat_type_id = c.cat_type ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON cl.cat_id = c.cat_ID AND cl.lang_id = '$locale'".
    			" WHERE $strpo c.active = '1' AND c.cat_parent='$cat' ORDER BY c.cat_position ");
		$res = 	$db->get_results($strSQL);	
    	return $res;
    }
	
	public static function get_catname_by_id($db,$id) {
		$locale = $_SESSION["LC_ALL"];
    	$strSQL = sprintf("SELECT cl.cat_name FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON cl.cat_id = c.cat_ID AND cl.lang_id = '$locale'".
    			" WHERE c.cat_ID 	='$id' ");
    	return $db->get_row($strSQL);
    }
	
	function getCategoryNameById($db, $id){
		$locale = $_SESSION["LC_ALL"];
    	$sql = sprintf("SELECT cat_name FROM %s ".
				" LEFT JOIN %2\$s ON %2\$s.cat_id = %1\$s.cat_ID AND %2\$s.lang_id = '$locale'".
				"where %1\$s.cat_ID 	='$id' ", 
				TBL_CATEGORY,
				TBL_CATEGORY_LANG);
		    	
    	$res = $db->get_row($sql);
    	if($res!=""){
					$res->cat_name = html_entity_decode($res->cat_name);
			}
    	return $res->cat_name;
    }
	
	public static function get_page_by_Url($db,$url) {
		$locale = $_SESSION["LC_ALL"];
    	$strSQL = sprintf("SELECT c.cat_ID,cl.cat_name,c.cat_parent,cl.cat_description,cl.cat_flash FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON c.cat_ID =  cl.cat_id AND cl.lang_id = '$locale'".
    			" WHERE cl.cat_url 	='$url' ");
		$res = $db->get_row($strSQL);
		if($res!=""){
					$res->cat_name = html_entity_decode($res->cat_name, ENT_QUOTES);
					$res->cat_description = html_entity_decode($res->cat_description, ENT_QUOTES);
			}
    	return $res;
    }
	
    public static function get_url_lang($db,$url,$locale) {
    	$strSQL = sprintf("SELECT c.cat_ID,cl.cat_url FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON c.cat_ID =  cl.cat_id AND cl.lang_id = '$locale'".
    			" WHERE cl.cat_ID 	= '(SELECT l.cat_id FROM  ".TBL_CATEGORY_LANG." l WHERE l.cat_url =  '$url'  )'");
		$res = $db->get_row($strSQL);
		
    	return $res;
    }
	
	function getCategoryByID($db,$ID){
		$locale = $_SESSION["LC_ALL"];
    	$sql = sprintf("SELECT cl.cat_name,cl.cat_logo,c.cat_ID,cl.cat_description  FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_TYPE." ct ON ct.cat_type_id = c.cat_type ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON cl.cat_id = c.cat_ID AND cl.lang_id = '$locale'".
				" WHERE c.cat_ID =".$ID." AND c.active = '1' ");
		    	
    	$res = $db->get_row($sql);
    	if($res!=""){
					$res->cat_name = html_entity_decode($res->cat_name);
					$res->cat_description = html_entity_decode($res->cat_description);
			}
    	return $res;
    }   
	
	function getCategoryByParent($db,$ID){
		$locale = $_SESSION["LC_ALL"];
    	$sql = sprintf("SELECT cl.cat_name,cl.cat_logo,c.cat_ID,cl.cat_description,cl.cat_url  FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_TYPE." ct ON ct.cat_type_id = c.cat_type ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON cl.cat_id = c.cat_ID AND cl.lang_id = '$locale'".
				" WHERE c.cat_parent =".$ID." AND c.active = '1' ");
		    	
    	$res = $db->get_results($sql);
    		if($res!=""){
				foreach($res as $item){
					$item->cat_name = html_entity_decode($item->cat_name);
				}
			}
    	return $res;
    }   
	
	function getHeaderCategoryByType($db,$type){
		$locale = $_SESSION["LC_ALL"];
    	$sql = sprintf("SELECT cm.manage_name,cl.cat_name,cl.cat_logo,c.cat_link,cl.cat_url,c.cat_ID  FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_TYPE." ct ON ct.cat_type_id = c.cat_type ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON cl.cat_id = c.cat_ID AND cl.lang_id = '$locale'".
				" LEFT JOIN ".TBL_CATEGORY_MANAGE." cm ON c.cat_manage = cm.id ".
				" WHERE c.cat_type =".$type." AND c.cat_parent = 0 AND c.active = '1' ORDER BY c.cat_position");
		    	
    	$res = $db->get_results($sql);
    		if($res!=""){
				foreach($res as $item){
					$item->cat_name = html_entity_decode($item->cat_name);
				}
			}
    	return $res;
    }   
	
	function countCategoryByTypeSearch($db,$lang,$type,$key){
		
		if($key!='')
			$cond = " AND (cl.cat_name like '%%$key%%' OR cl.cat_description like '%%$key%%')  AND cl.cat_description != ''";
    	$sql = sprintf("SELECT COUNT(c.cat_ID) as total  FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_TYPE." ct ON ct.cat_type_id = c.cat_type ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON cl.cat_id = c.cat_ID AND cl.lang_id = '$lang'".
				" WHERE c.cat_type =".$type."  AND c.active = '1' $cond");
		// echo  $sql;  	
    	$res = $db->get_row($sql);
    		
    	return $res->total;
    }   
	
	function getHeaderCategoryByTypeSearch($db,$lang,$share, $list,$type,$key){
		
		$page = isset ( $_GET[$list] ) ? intval ( $_GET[$list] ) : 1;
		$rows_per_page= $share;
		$page_start = ( $page - 1 ) * $rows_per_page;
		$page_end = $page * $rows_per_page;
		
		if($key!='')
			$cond = " AND (cl.cat_name like '%%$key%%' OR cl.cat_description like '%%$key%%') AND cl.cat_description != ''";
    	$sql = sprintf("SELECT cl.cat_name,cl.cat_logo,c.cat_link,cl.cat_description,c.cat_ID  FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_TYPE." ct ON ct.cat_type_id = c.cat_type ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON cl.cat_id = c.cat_ID AND cl.lang_id = '$lang'".
				" WHERE c.cat_type =".$type."  AND c.active = '1' $cond  LIMIT $page_start, $rows_per_page");
		    	
    	$res = $db->get_results($sql);
    		if($res!=""){
				foreach($res as $item){
					$item->cat_name = html_entity_decode($item->cat_name);
					$item->cat_description = html_entity_decode($item->cat_description);
				}
			}
    	return $res;
    }   
	
	function getHeaderCategoryByLink($db,$link){
		$locale = $_SESSION["LC_ALL"];
    	$sql = sprintf("SELECT cl.cat_name,cl.cat_logo,c.cat_link,c.cat_ID FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_TYPE." ct ON ct.cat_type_id = c.cat_type ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON cl.cat_id = c.cat_ID AND cl.lang_id = '$locale'".
				" WHERE c.cat_parent = (SELECT cb.cat_ID FROM ".TBL_CATEGORY." cb WHERE cb.cat_link LIKE '$link')".
				" AND c.active = '1' ORDER BY c.cat_position");
    	$res = $db->get_results($sql);
    	return $res;
    }   
	
	public static function get_manage_by_Url($db,$url) {
		$locale = $_SESSION["LC_ALL"];
    	$strSQL = sprintf("SELECT ct.manage_name,cl.cat_url FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON cl.cat_id = c.cat_ID AND cl.lang_id = '$locale'".		  
				" LEFT JOIN ".TBL_CATEGORY_MANAGE." ct ON c.cat_manage = ct.id ".
    			" WHERE cl.cat_url 	='$url' ");
		$res = $db->get_row($strSQL);
	
    	return $res;
    }
	
	public static function getCatbyURL($db,$caturl){
		$locale = $_SESSION["LC_ALL"];
    	$sql = sprintf("SELECT cl.cat_name,c.cat_ID,cl.cat_url,".
						"(SELECT la.cat_url  FROM ".TBL_CATEGORY_LANG." la ".
								" WHERE la.lang_id = '$locale' AND la.cat_id =  (SELECT lb.cat_id  FROM ".TBL_CATEGORY_LANG." lb WHERE lb.cat_url = '$caturl') ".
								") as parent_url ".
					" FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON cl.cat_id = c.cat_ID AND cl.lang_id = '$locale'".
				" WHERE c.cat_parent = (SELECT l.cat_id  FROM ".TBL_CATEGORY_LANG." l WHERE l.cat_url = '$caturl') AND c.active = '1' ");
		    	
    	$res = $db->get_results($sql);
    		if($res!=""){
				foreach($res as $item){
					$item->cat_name = html_entity_decode($item->cat_name);
				}
			}
    	return $res;
    }   
}
?>